﻿Data = {};

var SideBarAction = {
	initialize:function()
	{
		this.current_navi = $("li[id*='navi_'] > a[class='current_page']");
		this.another_navi = $("li[id*='navi_'] > a[class!='current_page']");
		
		this.registerHandlers();
	},
	registerHandlers:function()
	{
		this.onMouseAction();
	},
	onMouseAction:function()
	{
		$(this.another_navi).each(function(){
			var img_src = $("img", this).attr('src') + "";
			$(this).mouseover(function(){
				var background = "#F2F2F2 url('images/list_arrow_.png') 93% 55% no-repeat";
				var img_src_ch = img_src.substring(0, 15) + "RED.png";
				$(this).css('background', background);
				$("img", this).attr('src', img_src_ch);
				$(this).css('cursor', 'pointer');
			});
			$(this).mouseout(function(){
				$(this).css('background', '#FFFFFF');
				$("img", this).attr('src', img_src);
				$(this).css('cursor', 'default');
			});
		});
	}
};

var AnswerSheetAction = {
	initialize:function()
	{
		this.anotherTabs = $("a[id*='tab_'][class='another_tab']");
		this.currentTabs = $("a[id*='tab_'][class='current_tab']");
		this.q4CheckBoxes = $("input[name*='checker_04']");
		this.allButtons = $("div[id='help_img'] > a");
		this.allSelTexts = $("div[id='checked_point'] > div > span");
		this.noticeBefore = $("#before_notice");
		
		this.saveButton = $("#save");
		this.cancelButton = $(".cancel");
		this.showresultButton = $("#show_result");
		
		this.onTextInputDefault();
		this.onTabDefault();
		this.onCheckedPoint();
		
		this.registerHandlers();
	},
	initializeForTabs:function()
	{
		this.anotherTabs = $("a[id*='tab_'][class='another_tab']");
		this.currentTabs = $("a[id*='tab_'][class='current_tab']");
		
		this.registerTabHandlers();
	},
	registerHandlers:function()
	{	
		this.onSaveShowCancel();
		this.onButtonsChange();
		this.onButtonsClick();
		this.onMouseAction();
		this.onTabChange();
		this.onTextInputChange();
	},
	registerTabHandlers:function()
	{	
		this.onMouseAction();
		this.onTabChange();
	},
	onDataInitialize:function(){
		Data.answer1_body_L = new Array();
		Data.answer1_body_R = new Array();
		Data.answer1_hand_L = new Array();
		Data.answer1_hand_R = new Array();
		Data.answer1_foot_L = new Array();
		Data.answer1_foot_R = new Array();
		
		for (var i=0;i<6;i++){
			Data.answer1_body_L[i] = false;
			Data.answer1_body_R[i] = false;
		}
		for (var i=0;i<9;i++){
			Data.answer1_hand_L[i] = false;
			Data.answer1_hand_R[i] = false;
		}
		for (var i=0;i<5;i++){
			Data.answer1_foot_L[i] = false;
			Data.answer1_foot_R[i] = false;
		}
		
		Data.answer2 = "";
		Data.answer3_ck_list = new Array();
		Data.answer4_ck_list = new Array();
		Data.answer4_t_input = new Array();
		
		for (var i=0;i<4;i++){
			Data.answer3_ck_list[i] = 0;
			Data.answer4_ck_list[i] = 0;
			Data.answer4_t_input[i] = "";
		}
	},
	onMouseAction:function()
	{
		$(this.anotherTabs).each(function(){
			$(this).mouseover(function(){
				$(this).removeClass('another_tab').addClass('another_tab_mouseover');
			});
			$(this).mouseout(function(){
				$(this).removeClass('another_tab_mouseover');
				if (!$(this).hasClass('current_tab')){
					$(this).addClass('another_tab');
				}
			});
		});
		
	},
	onTabDefault:function(){
		//$("#as_tab_03_02").hide();
		$("#as_tab_04_02").hide();
	},
	onTabChange:function()
	{
		$(this.anotherTabs).each(function(){
			$(this).mousedown(function(){
				$(this).removeClass('another_tab_mouseover').addClass('current_tab');
				var this_id = $(this).attr('id');
				if (this_id.substr(0,6) == "tab_03"){
					if (this_id.substr(7,2) == "01"){
						$("#tab_03_02").removeClass('current_tab').addClass('another_tab');
						$("#as_tab_03_02").hide();
					}else{
						$("#tab_03_01").removeClass('current_tab').addClass('another_tab');
						$("#as_tab_03_01").hide();
					}
				}else{
					if (this_id.substr(7,2) == "01"){
						$("#tab_04_02").removeClass('current_tab').addClass('another_tab');
						$("#as_tab_04_02").hide();
					}else{
						$("#tab_04_01").removeClass('current_tab').addClass('another_tab');
						$("#as_tab_04_01").hide();
					}
				}
				$("#as_"+this_id).show();
			});
			$(this).mouseup(function(){
				$(this).unbind('mouseout');
				$(this).unbind('mouseover');
				AnswerSheetAction.initializeForTabs();
			});
		});
	},
	onTabContentChange:function(tabID)
	{
		if (tabID == "tab_03_01"){
			return '<ul>\
						<li>\
						아침에 관절이 뻣뻣합니다\
						<input type="checkbox" name="answer_03_01" value="1"/>\
					</li>\
					<li>\
						팔꿈치 또는 손가락에 덩어리가 만져집니다\
						<input type="checkbox" name="answer_03_02" value="1"/>\
					</li>\
					<li>\
						운동을 하면 금새 지칩니다\
						<input type="checkbox" name="answer_03_03" value="1"/>\
					</li>\
					<li>\
						손 또는 발이 저립니다\
						<input type="checkbox" name="answer_03_04" value="1"/>\
					</li>\
				</ul>';
		}
		else if (tabID == "tab_03_02"){
			return 'ANOTHER SYMPTOM';
		}
		else if (tabID == "tab_04_01"){
			return '<span class="checkbox_04">류마티스 지표</span>\
			<input type="checkbox" name="checker_04_01" value="1"/>RF 수치\
			<input type="checkbox" name="checker_04_02" value="1"/>ACPA 수치<br/>\
			<input type="text" name="textinput_04_01" value=" RF 수치를 입력하세요"/><br/>\
			<input type="text" name="textinput_04_02" value=" ACPA 수치를 입력하세요"/><br/>\
			<span class="checkbox_04">급성염증 지표</span>\
			<input type="checkbox" name="checker_04_03" value="1"/>CRP 수치\
			<input type="checkbox" name="checker_04_04" value="1"/>ESP 수치<br/>\
			<input type="text" name="textinput_04_03" value=" CRP 수치를 입력하세요"/><br/>\
			<input type="text" name="textinput_04_04" value=" ESP 수치를 입력하세요"/><br/>';
		}
		else if (tabID == "tab_04_02"){
			return 'NO';
		}
	},
	onTextInputDefault:function(){
		var defaultBoxSettings = $("input[name*='checker_04']:not(:checked)");
		defaultBoxSettings.each(function(){
			var dtextBoxName = "textinput" + $(this).attr('name').substr(7,6);
			var dtextBox = $("input[name='"+dtextBoxName+"']");
			dtextBox.hide();
		})
		var allInputBoxes = $("input[name*='textinput_04']");
		allInputBoxes.each(function(){
			$(this).one("click", function(){
				$(this).attr('value', '');
			});
		});
	},
	onTextInputChange:function(){		
		$(this.q4CheckBoxes).each(function(){
			$(this).click(function(){
				var textBoxName = "textinput" + $(this).attr('name').substr(7,6);
				var textBox = $("input[name='"+textBoxName+"']");
				if ($(this).attr('checked') == "checked"){
					textBox.show();
				}else{
					textBox.hide();
				}
			});
		});
	},
	onButtonsChange:function(){
		$(this.allButtons).children().each(function(){
			$(this).mouseover(function(){
				$(this).attr('src', 'images/dot_red.png');
			});
			if ($(this).parent().hasClass('selected')){
				$(this).unbind('mouseout');
			}else{
				$(this).mouseout(function(){
					$(this).attr('src', 'images/dot_blue.png');
				});
			}
		});
	},
	onButtonsClick:function(){
		$(this.allButtons).each(function(){
			$(this).click(function(){
				if ($(this).hasClass('selected')){
					var name = '.' + $(this).attr('id');					
					$(name).hide();
					
					$(this).removeClass('selected');
					$(this).children().attr('src', 'images/dot_blue.png');
				}else{
					(AnswerSheetAction.noticeBefore).hide();
					var name = '.' + $(this).attr('id');
					
					$(name).show();
					
					$(this).addClass('selected');
					$(this).children().attr('src', 'images/dot_red.png');
				}
				AnswerSheetAction.onButtonsChange();
			});
		});
	},
	onCheckedPoint:function(){
		$(this.allSelTexts).each(function(){
			$(this).hide();
			(AnswerSheetAction.noticeBefore).html('파란색 점을 누르시면,<br>체크가 됩니다.<br>그리고 다시 누르시면,<br>체크가 풀립니다.<br>');
		});
	},
	onSaveShowCancel:function(){
		(this.cancelButton).each(function(){
			$(this).click(function(){
				location.reload();
			});
		});
		(this.showresultButton).click(function(){
			//onSaveData();

			
			area = "";

			//큰관절
			for(i=1;i<=6;i++)
			{
				if(i==3)
					continue;
				if($("#body_L_0"+i).hasClass("selected")) 
					area += "1";
				else
					area += "0";

			}
			for(i=1;i<=6;i++)
			{
				if(i==3)
					continue;
				if($("#body_R_0"+i).hasClass("selected")) 
					area += "1";
				else
					area += "0";

			}

			//작은관절
			if($("#body_L_03").hasClass("selected")) 
				area += "1";
			else
				area += "0";
			if($("#body_R_03").hasClass("selected")) 
				area += "1";
			else
				area += "0";


			for(i=1;i<=9;i++)
			{
				if($("#hand_L_0"+i).hasClass("selected")) 
					area += "1";
				else
					area += "0";
				if($("#hand_R_0"+i).hasClass("selected")) 
					area += "1";
				else
					area += "0";

			}

			for(i=1;i<=5;i++)
			{
				if($("#foot_L_0"+i).hasClass("selected")) 
					area += "1";
				else
					area += "0";
				if($("#foot_R_0"+i).hasClass("selected")) 
					area += "1";
				else
					area += "0";

			}

			//기타관절 - 생략됨
			area += "0000000000000";


			if($("input[name='answer_02_yes_no']")[0].checked)
				over_6weeks = "1";
			else
				over_6weeks = "0";

			if($("input[name='answer_03_01']").attr("checked")=="checked")
				s1 = "1";
			else
				s1 = "0";
			if($("input[name='answer_03_02']").attr("checked")=="checked")
				s2 = "1";
			else
				s2 = "0";
			if($("input[name='answer_03_03']").attr("checked")=="checked")
				s3 = "1";
			else
				s3 = "0";
			if($("input[name='answer_03_04']").attr("checked")=="checked")
				s4 = "1";
			else
				s4 = "0";
			if($("input[name='answer_03_05']").attr("checked")=="checked")
				s5 = "1";
			else
				s5 = "0";
			if($("input[name='answer_03_06']").attr("checked")=="checked")
				s6 = "1";
			else
				s6 = "0";
			if($("input[name='answer_03_07']").attr("checked")=="checked")
				s7 = "1";
			else
				s7 = "0";

			age = $("#age").val();


			if($("input[name='checker_04_01']").attr("checked")=="checked")
				know_rf = "1";
			else
				know_rf = "0";
			if($("input[name='checker_04_02']").attr("checked")=="checked")
				know_acpa = "1";
			else
				know_acpa = "0";
			if($("input[name='checker_04_03']").attr("checked")=="checked")
				know_crp = "1";
			else
				know_crp = "0";
			if($("input[name='checker_04_04']").attr("checked")=="checked")
				know_esr = "1";
			else
				know_esr = "0";
			
			rf = $("#textinput_04_01").val();
			acpa = $("#textinput_04_02").val();
			crp = $("#textinput_04_03").val();
			esr = $("#textinput_04_04").val();


			$.post(
				"4_5_add.php", 
				{	
					'age': age,
					'area' : area,
					'over_6weeks' : over_6weeks,
					's1' : s1,
					's2' : s2,
					's3' : s3,
					's4' : s4,
					's5' : s5,
					's6' : s6,
					's7' : s7,
					'know_rf' : know_rf,
					'rf' : rf,
					'know_acpa' : know_acpa,
					'acpa' : acpa,
					'know_crp' : know_crp,
					'crp' : crp,
					'know_esr' : know_esr,
					'esr' : esr
				}, 
				function (data) { 
					if(data=="1")
						//입력성공
						location.href = '4_5_proc.php';
					
				}
			); 
			/*
			age = '$age',


	age = '$age',
	area = '$area',
	over_6weeks = '$over_6weeks',
	symptom_1 = '$s1',
	symptom_2 = '$s2',
	symptom_3 = '$s3',
	symptom_4 = '$s4',
	symptom_5 = '$s5',
	symptom_6 = '$s6',
	symptom_7 = '$s7',
	know_rf = $know_rf, rf = '$rf' , know_acpa = $know_acpa, acpa = $acpa,
	know_crp = $know_crp, crp = '$rf' , know_esr = $know_esr, esr = $esr
*/
//			alert("show RESULT!");
		});
		/* 삭제 2011-11-27 ht
		(this.saveButton).click(function(){
			//onSaveData();
			alert("save DATA!");
		});
		*/
	},
	onSaveData:function(){
		Data.answer1_body_L[0] = $("#body_L_01").hasClass("selected");
		Data.answer1_body_L[1] = $("#body_L_02").hasClass("selected");
		Data.answer1_body_L[2] = $("#body_L_03").hasClass("selected");
		Data.answer1_body_L[3] = $("#body_L_04").hasClass("selected");
		Data.answer1_body_L[4] = $("#body_L_05").hasClass("selected");
		Data.answer1_body_L[5] = $("#body_L_06").hasClass("selected");
	
		Data.answer1_body_R[0] = $("#body_R_01").hasClass("selected");
		Data.answer1_body_R[1] = $("#body_R_02").hasClass("selected");
		Data.answer1_body_R[2] = $("#body_R_03").hasClass("selected");
		Data.answer1_body_R[3] = $("#body_R_04").hasClass("selected");
		Data.answer1_body_R[4] = $("#body_R_05").hasClass("selected");
		Data.answer1_body_R[5] = $("#body_R_06").hasClass("selected");
		
		Data.answer1_hand_L[0] = $("#hand_L_01").hasClass("selected");
		Data.answer1_hand_L[1] = $("#hand_L_02").hasClass("selected");
		Data.answer1_hand_L[2] = $("#hand_L_03").hasClass("selected");
		Data.answer1_hand_L[3] = $("#hand_L_04").hasClass("selected");
		Data.answer1_hand_L[4] = $("#hand_L_05").hasClass("selected");
		Data.answer1_hand_L[5] = $("#hand_L_06").hasClass("selected");
		Data.answer1_hand_L[6] = $("#hand_L_07").hasClass("selected");
		Data.answer1_hand_L[7] = $("#hand_L_08").hasClass("selected");
		Data.answer1_hand_L[8] = $("#hand_L_09").hasClass("selected");

		Data.answer1_hand_R[0] = $("#hand_R_01").hasClass("selected");
		Data.answer1_hand_R[1] = $("#hand_R_02").hasClass("selected");
		Data.answer1_hand_R[2] = $("#hand_R_03").hasClass("selected");
		Data.answer1_hand_R[3] = $("#hand_R_04").hasClass("selected");
		Data.answer1_hand_R[4] = $("#hand_R_05").hasClass("selected");
		Data.answer1_hand_R[5] = $("#hand_R_06").hasClass("selected");
		Data.answer1_hand_R[6] = $("#hand_R_07").hasClass("selected");
		Data.answer1_hand_R[7] = $("#hand_R_08").hasClass("selected");
		Data.answer1_hand_R[8] = $("#hand_R_09").hasClass("selected");
		
		Data.answer1_foot_L[0] = $("#foot_L_01").hasClass("selected");
		Data.answer1_foot_L[1] = $("#foot_L_02").hasClass("selected");
		Data.answer1_foot_L[2] = $("#foot_L_03").hasClass("selected");
		Data.answer1_foot_L[3] = $("#foot_L_04").hasClass("selected");
		Data.answer1_foot_L[4] = $("#foot_L_05").hasClass("selected");
		
		Data.answer1_foot_R[0] = $("#foot_R_01").hasClass("selected");
		Data.answer1_foot_R[1] = $("#foot_R_02").hasClass("selected");
		Data.answer1_foot_R[2] = $("#foot_R_03").hasClass("selected");
		Data.answer1_foot_R[3] = $("#foot_R_04").hasClass("selected");
		Data.answer1_foot_R[4] = $("#foot_R_05").hasClass("selected");

		Data.answer2 = $("input[name='answer_02_yes_no']").val();
		
		Data.answer3_ck_list[0] = $("input[name='answer_03_01']").val();
		Data.answer3_ck_list[1] = $("input[name='answer_03_02']").val();
		Data.answer3_ck_list[2] = $("input[name='answer_03_03']").val();
		Data.answer3_ck_list[3] = $("input[name='answer_03_04']").val();
		Data.answer3_ck_list[4] = $("input[name='answer_03_05']").val();
		Data.answer3_ck_list[5] = $("input[name='answer_03_06']").val();
		Data.answer3_ck_list[6] = $("input[name='answer_03_07']").val();
		
		Data.answer4_ck_list[0] = $("input[name='checker_04_01']").val();
		Data.answer4_ck_list[1] = $("input[name='checker_04_02']").val();
		Data.answer4_ck_list[2] = $("input[name='checker_04_03']").val();
		Data.answer4_ck_list[3] = $("input[name='checker_04_04']").val();
		
		if ($("#as_tab_04_01").attr('class') == 'current_tab'){
			if(Data.answer4_ck_list[0] == 1){
				Data.answer4_t_input[0] == $("input[name='textinput_04_01']").val();
			}else{Data.answer4_t_input[0] == "";}
			if(Data.answer3_ck_list[1] == 1){
				Data.answer4_t_input[1] == $("input[name='textinput_04_02']").val();
			}else{Data.answer4_t_input[1] == "";}
			if(Data.answer3_ck_list[2] == 1){
				Data.answer4_t_input[2] == $("input[name='textinput_04_03']").val();
			}else{Data.answer4_t_input[2] == "";}
			if(Data.answer3_ck_list[3] == 1){
				Data.answer4_t_input[3] == $("input[name='textinput_04_04']").val();
			}else{Data.answer4_t_input[3] == "";}
		}
	}
};

var IEBrowser = {
    versionCheck:function()
    {
        var version = 999; // we assume a sane browser
        if (navigator.appVersion.indexOf("MSIE") != -1)
          // bah, IE again, lets downgrade version number
          version = parseFloat(navigator.appVersion.split("MSIE")[1]);
        return version;
    },
    onUnavailableIE:function()
    {
        var ie_version = this.versionCheck();
        if(ie_version < 7){
            alert("브라우저의 버전이 너무 낮습니다.\n업그레이드 후, 이용해주십시오.\n확인을 누르시면,\n자동으로 IE 버전 업그레이드 페이지로 이동합니다.");
            location.replace("http://windows.microsoft.com/ko-KR/internet-explorer/products/ie/home");
        }
    }
};
